//获取应用实例
var app = getApp();

// 初始化数据
var init = {

  // 数据
  data: {

    // 分页参数
    page: {

      // 当前页码
      index: 1,

      // 每页分页数
      size: 10,

      // 分页总数
      num: 1,

      // 加载锁
      lock: false
    },

    // 产品列表
    items: [],

    // 加载提示
    loadTips: '',

    // 订单状态
    status: '',
    cate_name:'',
    sex:'',
    age:'',
    act:'',

  },

  // 页面第一次加载
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          height: res.windowHeight
        })
      }
    })
    console.log(options);
    that.setData({
      status: options.status || '',
      options:options
    })
    if(options.keywords!=undefined){

      var a = options.keywords.split("  ").length;
      if(a==3){
        that.setData({
          cate_name: options.keywords.split("  ")[0],
          sex: options.keywords.split("  ")[1],
          age: options.keywords.split("  ")[2],
          act:'getOtherArticleList',
          keywords:''
        })
      }else{
        that.setData({
          keywords: options.keywords,
          act:'getSearchList'
        })
      }
    }else{
      that.setData({
        cate_name: options.cate_name,
        sex: options.sex,
        age: options.age.split("岁")[0],
        keywords:'',
        act:'getOtherArticleList'
      })
    }
    that.getData();
  },

  // 上拉触底
  lower: function () {
    this.getData();
  },

  // 切换tab
  tab: function (event) {

    var that = this;
    var status = event.currentTarget.dataset.status;
    console.log(that.data.page.lock);

    that.setData({
      status: status,
      page: {
        index: 1,
        size: 10,
        num: 1

      },
      items: []
    });
    that.getData();

  },
  // 获取数据
  getData: function () {

    var that = this;
    var page = that.data.page;

    // 加锁
    if (page.lock) return;
    that.setData({
      'page.lock': true
    });

    app.getLoginSession(function (session) {

      if (page.num >= page.index) {

        // 加载前
        that.setData({
          loading: false,
          loadTips: that.data.loading ? '' : '加载中...'
        });


        wx.request({

          // 请求地址
          url: app.api,

          // 请求参数
          data: {
            app: 'article',
            act: that.data.act,
            cate_name:that.data.cate_name,
            sex:that.data.sex,
            age:that.data.age,
            keywords:that.data.keywords,
            page: page.index,
            t_session: session
          },

          // 请求成功
          success: function (res) {
            var data = res.data;
            console.log(data);


            that.setData({
              'page.num': parseInt(Math.ceil(data.retval.total_count / that.data.page.size))
            });

            console.log('总页码：' + that.data.page.num + '，当前页码：' + that.data.page.index);

            // 完全没有数据
            if (that.data.page.num == 0) {
              that.setData({
                loadTips: '暂无数据！'
              })
              return
            }

            // 有数据，累加
            var items = data.retval.articleList;
            if (items && items.length) {
              that.setData({
                items: that.data.items.concat(items)
              })
            }

            // 如果只有一页数据
            if (that.data.page.num == 1) {
              that.setData({
                loadTips: '无更多数据！'
              })
            }

            // 累加页码
            that.setData({
              'page.index': ++page.index
            });

          },

          // 请求失败
          fail: function () {
            console.log('aa');
            that.setData({
              loading: true,
              loadTips: '加载数据失败！'
            })
          },

          // 请求结束
          complete: function () {

            // 解锁
            that.setData({
              'page.lock': false
            })
          }

        });
      } else {
        that.setData({
          loadTips: '无更多数据！'
        })
      }
    })
  }

};

// 注册页面
Page(init);
